'''
Created on Apr 27, 2010

@author: william
'''
import config_read
import sys_state
import sys
import os
import logging
import logging.handlers

def main():
    '''
    The main execution point for the agent
    '''
    load_config()
    init_logger()

def load_config():
    ## Loads the configuration file into memory
    try:
        config = config_read.load(str(sys.argv[1])) # If an argument is specified, it uses that path
    except:
        print "No configuration file specified, trying to load default config"
        config = config_read.load('conf/client_settings.cfg') # If no argument, the default is used
    
    print config
    
def init_logger(logLevel="debug", logFile="log/agent.log", loggerName="agent"):
        LEVELS = {'debug': logging.DEBUG,
                  'info': logging.INFO,
                  'warning': logging.WARNING,
                  'error': logging.ERROR,
                  'critical': logging.CRITICAL}
        
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        
        # Sanitize the logLevel
        logLevel = logLevel.lower()
        
        # Set up the logger object with the base name of 'agent' and a default
        #    logLevel of logging.INFO if no logLevel is specified
        log = logging.getLogger(loggerName) # The logger name should be based on the config file
        log.setLevel(LEVELS[logLevel])
        
        fh = logging.FileHandler(logFile)
        
        if (logLevel == 'debug'):
            ch = logging.StreamHandler()
            ch.setLevel(LEVELS[logLevel])
            ch.setFormatter(formatter)
            log.addHandler(ch)
        
        
        fh.setFormatter(formatter)
        log.addHandler(fh)
        
        log.info("initializing logger")

if __name__ == "__main__":
    main()